<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 1.引入vue.js -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>

<body>
    <div id="app">
        <!-- 1 -->
        <p>这是原数据{{ message }}</p>
        <p>这是计算属性获得的数据 {{ newMessage }}</p>
        <!-- 2 -->
        <input type="text" v-model="xing">
        <br /><br />
        <input type="text" v-model="ming">
        <br /><br />
        <input type="text" v-model="quanming">
        <!-- 3 -->
        <div>今天的天气真{{ this.tianqi ? "晴朗" : "差劲" }}</div>
        <div>{{ num }}</div>
        <button @click="qiehuan">切换</button>
    </div>

    <script>
        let app = new Vue({
            el: '#app',
            data: {
                message: 'ABCDEFG',
                xing: '',
                ming: '',
                tianqi: true,
                num: 1
            },
            methods: {
                qiehuan() {
                    this.tianqi = !this.tianqi
                    this.num++
                }
            },
            // 计算属性具有缓存
            computed: {
                // 1-简单写法
                newMessage() {
                    return this.message.split('').reverse().join('')
                },
                // 2-完整写法(get和set用法)
                quanming: {
                    get() {
                        return this.xing + ' ' + this.ming
                    },
                    set(value) {
                        let newValue = value.split(' ')
                        this.xing = newValue[0]
                        this.ming = newValue[1]
                    }
                }
            },

            // 侦听属性
            watch: {
                // 1.简写形式,省掉handler
                tianqi(newvalue, oldvalue) {
                    console.log(newvalue, oldvalue);
                },
                //   2.完整写法,可以添加更多配置
                num: {
                    // immediate:true, // 初始化时让handler调用一下
                    // deep:true,    // 深度监视
                    handler(newvalue, oldvalue) {
                        console.log(newvalue, oldvalue);
                    }
                }


            }

        })
    </script>

</body>

</html>